import {baseApiF} from './api'

let baseApi = baseApiF();

// 用户列表
const getUserList = function (reqData, pageNum, pageSize) {
  let url = baseApi + 'user/find?pageNum=' + pageNum + '&pageSize=' + pageSize;
  const data = Object.assign({}, reqData);
  return axiosFun(url, data)
};

// 查询所属部门
const getListDept = function () {
  let url =baseApi+'dept/findAll';
  return axiosFun(url)
};

// 停用启用
const updateIsRun = function (userId, isRun) {
  let url = baseApi + 'user/updateIsRun?userId=' + userId + '&isRun=' + isRun;
  return axiosFun(url);
};

// 添加用户时获取所有职位，部门等
const getUnitDeptJob = function () {
  let url = baseApi + 'user/findUnitDeptJob';
  return axiosFun(url)
};

// 添加用户  updateUser
const addUser = function (reqData) {
  let url = baseApi + 'user/add';
  const data = Object.assign({}, reqData);
  return axiosFun(url, data)
};

// 编辑用户
const updateUser = function (reqData) {
  let url = baseApi + 'user/update';
  const data = Object.assign({}, reqData);
  return axiosFun(url, data)
};

// 用户列表重置密码按钮
const updatePwd = function (userId) {
  let url = baseApi + 'user/resetPwd?userId=' + userId;
  return axiosFun(url)
};

// 删除用户
const delUser = function (userId) {
  let url = baseApi + 'user/del?userId=' + userId;
  return axiosFun(url)
};

// 获取角色权限
const getUserRole = function (userId) {
  let url = baseApi + 'user/findUserRole?userId=' + userId;
  return axiosFun(url)
};

// 保存角色权限
const saveUserRole = function (userId, roleIds) {
  let url = baseApi + 'user/saveUserRole?userId=' + userId + '&roleIds=' + roleIds;
  return axiosFun(url)
};

// 登录
const loginRequest = function (reqData) {
  return axiosUrlAndData('login', reqData);
};

// 注销登录
const logout = function () {
  return axiosUrlAndData('logout');
};

// 修改密码
const resetPwd = function (reqData) {
  return axiosUrlAndData('updatePassword',reqData);
};

// 角色添加
const addRoleRequest = function (reqData) {
  return axiosUrlAndData('role/add', reqData);
};

// 角色删除
const delRoleRequest = function (reqData) {
  return axiosUrlAndData('role/delRole', reqData);
};

// 启用角色
const updateIsRunRequest = function (reqData) {
  return axiosUrlAndData('role/updateIsRun', reqData);
};

// 角色列表
const getRoleListRequest = function (reqData) {
  return axiosUrlAndData('role/roleList', reqData);
};

// 编辑角色
const updateRoleRequest = function (reqData) {
  return axiosUrlAndData('role/update', reqData);
};

// 获取角色权限
const getRoleMenuRequest = function (reqData) {
  return axiosUrlAndData('role/roleMenu', reqData);
};

// 保存角色权限
const saveRoleMenuRequest = function (reqData) {
  return axiosUrlAndData('role/saveRoleMenu', reqData);
};
// 头部获取用户信息
const getUserInfo = function (reqData) {
  return axiosUrlAndData('querySelfInfo', reqData);
};

// 字数太长省略
const filterLogText = function (value, leng = 7) {
  if (value.length <= leng) {
    return value;
  } else {
    return value.slice(0, leng) + '...';
  }
};

// 发axios请求
function axiosFun (url, data = {}) {
  return window.axios.post(url, data)
    .then((res) => {
      return Promise.resolve(res.data)
    })
    .catch((error) => {
      console.log(JSON.stringify(error));
      return Promise.reject(error)
    })
}

function axiosUrlAndData (url, reqData) {

  if (!url.length) {
    console.log('没有接口名称');
    return;
  }

  let reqUrl = baseApi + url;
  const data = Object.assign({}, reqData);
  return axiosFun(reqUrl, data);
}

export {
  getUserList,
  getListDept,
  updateIsRun,
  filterLogText,
  loginRequest,
  logout,
  getUnitDeptJob,
  addUser,
  updateUser,
  updatePwd,
  delUser,
  getUserRole,
  saveUserRole,
  addRoleRequest,
  delRoleRequest,
  updateIsRunRequest,
  getRoleListRequest,
  updateRoleRequest,
  getRoleMenuRequest,
  saveRoleMenuRequest,
  resetPwd,
  getUserInfo,
}

